package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.common.util.web.WebUtil;
import com.deyuanyun.pic.ctrl.controller.vo.query.EqptQueryVO;
import com.deyuanyun.pic.ctrl.controller.vo.query.OperatorRecordsQueryVO;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.enumeration.OperationRecordEnum;
import com.deyuanyun.pic.ctrl.service.OperationRecordsService;
import com.deyuanyun.pic.settings.controller.BaseController;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 *  阴保运行管理---运行记录controller
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-18
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Controller
@RequestMapping("/corrctrl/operRecords")
public class OperationRecordsController extends BaseController {

    @Autowired
    private OperationRecordsService operationRecordsService;

    /**
     * 跳转到运行记录的设备基础信息页面
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-18
     */
    @RequestMapping("/toOperation.html")
    public String toOperation() {

        return "/corrctrl/cathodic/runRecord";
    }

    /**
     * 跳转到运行记录的设备运行记录页面
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-18
     */
    @RequestMapping("/toOperationRecords.html")
    public ModelAndView toOperationRecords() {
        return new ModelAndView("/corrctrl/cathodic/runRecordDetails");
    }

    /**
     * 查询阴保电源设备
     * @param 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-19
     */
    @RequestMapping("/eqptCppower.json")
    @ResponseBody
    public void queryEqptCppower(EqptQueryVO eqptQueryVO) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(operationRecordsService.queryCppowers(eqptQueryVO));
        AjaxSupport.sendSuccessText("success","yyyy-MM-dd", jsonBean);
    }

    /**
     * 查询运行记录
     * @param
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-19
     */
    @RequestMapping("/recode{type}.json")
    @ResponseBody
    public void queryRecodeDrainages(OperatorRecordsQueryVO eqptQueryVO,@PathVariable("type") String type) {
        OperationRecordEnum recordEnum = OperationRecordEnum.getInstance(type);
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(operationRecordsService.queryRecodes(eqptQueryVO, recordEnum.ordinal()));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 运行记录excel导出
     * @param
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-19
     */
    @RequestMapping("/exportRecode{type}.json")
    @ResponseBody
    public void exportRecodeCppowers(HttpServletResponse response, OperatorRecordsQueryVO eqptQueryVO,
                                     @PathVariable("type") String type) throws IOException {
        OperationRecordEnum recordEnum = OperationRecordEnum.getInstance(type);
        int index = recordEnum.ordinal();
        String filePath = recordEnum.getExpExcelPath();
        List list = operationRecordsService.queryRecodes(eqptQueryVO, index);
        List<String[]> listSheet = JXL.toListFormVO(list, "yyyyMMdd");
        Integer beginRow = recordEnum.getTempExpIndex();
        Workbook workbook = ExcelUtil.initWorkbook(filePath, listSheet, beginRow - 2);//-2是因为删除了模板前两行
        String excelName = StringUtils.convertObjectToString(eqptQueryVO.getCustomNumber(), "") +
                recordEnum.getExpName() + LocalDate.now().toString("yyyyMMdd") +
                filePath.substring(filePath.lastIndexOf("."), filePath.length());
        DownExcelUtil.fillWorkbookHead(workbook);
        WebUtil.setDownHeader(excelName);
        workbook.write(response.getOutputStream());
    }

    /**
     * 跳转到阴保电源设备运行记录统计
     * @author lijun
     * @date 2016-10-08
     */
    @RequestMapping("/toRecodeCppowersCount.html")
    public ModelAndView toRecodeCppowersCount(HttpServletRequest request) {
        HashMap<String, Object> param = RequestUtil.parseRequestParamToHashMap(request);

        return new ModelAndView("/corrctrl/cathodic/runRecordChart/cathodicChart",param);
    }
    /**
     * 跳转到测试桩设备运行记录统计
     * @author lijun
     * @date 2016-10-08
     */
    @RequestMapping("/toRecodeCptestCount.html")
    public ModelAndView toRecodeCptestCount(HttpServletRequest request) {
        HashMap<String, Object> param = RequestUtil.parseRequestParamToHashMap(request);

        return new ModelAndView("/corrctrl/cathodic/runRecordChart/testPileChart",param);
    }

    /**
     * 设备运行记录统计
     * @param
     * @author lijun
     * @date 2016-10-09
     */
    @RequestMapping("/queryEqptRecordCount.json")
    @ResponseBody
    public  void  queryEqptRecordCount(EqptTypeEnum eqptType, OperatorRecordsQueryVO recordsBaseVO){
        Asserts.assertTrue(StringUtils.isNotNullEmpty(recordsBaseVO.getDeviceId()), "没有获取到设备Id");
        Asserts.assertTrue(StringUtils.isNotNullEmpty(eqptType.getLabel()), "没有获取到需要导出的设备类型");
        AjaxSupport.sendSuccessText("",operationRecordsService.queryEqptRecordCount(eqptType,recordsBaseVO));
    }
}
